package com.app.team.liquid;

//============================================================================
//Name        : ForumMenu.java
//Author      : Kyle Bartush
//Version     : 0.1
//Description : Array adapter made for different threads within a
//				list of threads.
//============================================================================

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ForumThreadArrayAdapter extends ArrayAdapter<ForumThread> {

	private static final String tag = "ForumThreadArrayAdapter";
	private static final String ASSETS_DIR = "images/";
	private Context context;
	private ImageView threadIcon;
	private TextView threadTitle;
	private TextView threadAuthor;
	private List<ForumThread> threads = new ArrayList<ForumThread>();

	public ForumThreadArrayAdapter(Context context, int textViewResourceId,
			List<ForumThread> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.threads = objects;
	}

	public int getCount() {
		return this.threads.size();
	}

	public ForumThread getItem(int index) {
		return this.threads.get(index);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (row == null) {
			Log.d(tag, "Starting XML row Inflation.... ");
			LayoutInflater inflater = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.thread_listitem, parent, false);
			Log.d(tag, "Sucessfully completed XML Row Inflation!");
		}

		ForumThread thread = getItem(position);

		threadIcon = (ImageView) row.findViewById(R.id.thread_icon);

		threadTitle = (TextView) row.findViewById(R.id.thread_title);

		threadAuthor = (TextView) row.findViewById(R.id.thread_author);

		threadTitle.setText(thread.title);

		setThreadIcon(thread.icon);

		threadAuthor.setText(thread.author);

		return row;
	}

	private void setThreadIcon(String icon) {
		Bitmap bitmap;
		if (icon.contains("hot")) {
			bitmap = BitmapFactory.decodeResource(this.context.getResources(),
					R.drawable.hot);
		} else if (icon.contains("sticky")) {
			bitmap = BitmapFactory.decodeResource(this.context.getResources(),
					R.drawable.sticky);
		} else {
			bitmap = BitmapFactory.decodeResource(this.context.getResources(),
					R.drawable.regular);
		}
		threadIcon.setImageBitmap(bitmap);
	}

}
